//Written by Ting-Kai Liu, Indiana University School of Medicine

//Some open source codes Written in SMS2 by Paul Stothard, University of Alberta, Canada, was used under GNU General Public License.

function IUPAC_CodesToBase32Codes (theDocument) {

    	var newDna = "";
    	var maxInput = 500000;

	if (testScript() == false) {
		return false;
	}

   if ((checkFormElement (theDocument.forms[0].elements[0]) == false) || (checkTextLength(theDocument.forms[0].elements[0].value, maxInput) == false))	{
		return false;
   }	        
	
   var re = new RegExp ("[^gatcryswkmbdhvnxGATCRYSWKMBDHVNX]","g");
   newDna = theDocument.forms[0].elements[0].value.replace(/[^\w\-]/g,"");
   newDna = newDna.replace(re,theDocument.forms[0].elements[4].options[theDocument.forms[0].elements[4].selectedIndex].value);
	newDna = newDna.toUpperCase();
	newDna = newDna.replace(/A/g,"1");
	newDna = newDna.replace(/C/g,"2");
	newDna = newDna.replace(/M/g,"3");
	newDna = newDna.replace(/G/g,"4");
	newDna = newDna.replace(/R/g,"5");
	newDna = newDna.replace(/S/g,"6");
	newDna = newDna.replace(/V/g,"7");
	newDna = newDna.replace(/T/g,"8");
	newDna = newDna.replace(/W/g,"9");
	newDna = newDna.replace(/Y/g,"a");
	newDna = newDna.replace(/H/g,"b");
	newDna = newDna.replace(/K/g,"c");
	newDna = newDna.replace(/D/g,"d");
	newDna = newDna.replace(/B/g,"e");
	newDna = newDna.replace(/N/g,"f");
	newDna = newDna.replace(/-/g,"g");
	newDna = newDna.replace(/X/g,"0");
					
	openWindow("Base-32 nucleotide codes");
	openPre();
	outputWindow.document.write ('&gt;filtered DNA sequence consisting of ' + newDna.length + ' bases.\n');
	outputWindow.document.write (addReturns(newDna));
	outputWindow.document.write ("\n");	
	closePre();
	closeWindow();
    	return true;
}
